# -*- coding:UTF-8 -*-

"""
用于从xls文件中读取通讯录
"""

import xlrd
import json

class xlsfile():
    def __init__(self):
        self.file = xlrd.open_workbook('tongxunlu.xls')

    def get_rows(self):
        table = self.file.sheet_by_index(0)
        nrows = table.nrows
        rows = []
        for n in range(nrows):
            rows.append(
                {'id':int(table.row_values(n)[0]),
                 'nameCompy':table.row_values(n)[2]+'('+table.row_values(n)[1]+')',
                 'phone':str(int(table.row_values(n)[3])),
                 })
        return rows

    def get_rows_json(self):
        rows = self.get_rows()
        rows_json = json.dumps(rows)
        return rows_json

if __name__ == '__main__':
    x = xlsfile()
    rows = x.get_rows()
    print x.get_rows_json()

